package base.day07;

/**
 * 描述：
 *      用字典树查找公共字符串的前缀
 * @author hl
 * @version 1.0
 * @date 2020/10/29 11:16
 */
public class Code_03_TrieTree {

    public static class TrieNode {
        int path;
        int end;
        TrieNode[] nexts = new TrieNode[26];
    }

    public static class Trie {
        private TrieNode root;

        public Trie() {
            this.root = new TrieNode();
        }

        public void insert(String word) {
            if (word == null) {
                return ;
            }
            char[] chs = word.toCharArray();
            TrieNode node = root;
            for (int i = 0; i < chs.length; i++) {
                int next = chs[i] - 'a';
                if (node.nexts[next] == null) {
                    node.nexts[next] = new TrieNode();
                }
                node = node.nexts[next];
                node.path++;
            }
            node.end++;
        }

        public int search(String word) {
            if (word == null) {
                return 0;
            }
            char[] chs = word.toCharArray();
            TrieNode node = root;
            for (int i = 0; i < chs.length; i++) {
                int next = chs[i] - 'a';
                if (node.nexts[next] == null) {
                    return 0;
                }
                node = node.nexts[next];
            }
            return node.end;
        }

        public void delete(String word) {
            if (search(word) > 0) {
                char[] chs = word.toCharArray();
                TrieNode node = root;
                for (int i = 0; i < chs.length; i++) {
                    int next = chs[i] - 'a';
                    if (--node.nexts[next].path == 0) {
                        node.nexts[next] = null;
                        return;
                    }
                    node = node.nexts[next];
                }
                node.end--;
            }
        }

        public int prefixNumber(String pre) {
            if (pre == null) {
                return 0;
            }
            char[] chs = pre.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';
                if (node.nexts[index] == null) {
                    return 0;
                }
                node = node.nexts[index];
            }
            return node.path;
        }
    }


    public static void main(String[] args) {
        Trie trie = new Trie();
        System.out.println(trie.search("zuo"));
        trie.insert("zuo");
        System.out.println(trie.search("zuo"));
        trie.delete("zuo");
        System.out.println(trie.search("zuo"));
        trie.insert("zuo");
        trie.insert("zuo");
        trie.delete("zuo");
        System.out.println(trie.search("zuo"));
        trie.delete("zuo");
        System.out.println(trie.search("zuo"));
        trie.insert("zuoa");
        trie.insert("zuoac");
        trie.insert("zuoab");
        trie.insert("zuoad");
        trie.delete("zuoa");
        System.out.println(trie.search("zuoa"));
        System.out.println(trie.prefixNumber("zuo"));

    }
}
